//-----------------------------------------------------------------------
// <copyright file="ItemsHelper.cs" company="LBi">
//     Copyright (c) . All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

using System;
using Sitecore.Data.Fields;
using Sitecore.Data.Items;
using Sitecore.Diagnostics;
using Sitecore.Links;

namespace ColorBox4Sitecore.Helpers
{
    /// <summary>
    /// This helper class allow several generic function on Sitecore Items
    /// </summary>
    public static class ItemsHelper
    {
        /// <summary>
        /// Return Url from a sitecore general link
        /// </summary>
        /// <param name="item">Sitecore item</param>
        /// <param name="fieldName">Sitecore field name</param>
        /// <returns>item url or external url if linkField is external</returns>
        public static string GetUrlFromGeneralLink(this Item item, string fieldName)
        {
            return item.GetUrlFromGeneralLink(fieldName, "#");
        }

        /// <summary>
        /// Return Url from a sitecore general link
        /// </summary>
        /// <param name="item">Sitecore item</param>
        /// <param name="fieldName">Sitecore field name</param>
        /// <param name="defaultValue"></param>
        /// <returns>item url or external url if linkField is external</returns>
        public static string GetUrlFromGeneralLink(this Item item, string fieldName, string defaultValue)
        {
            LinkField linkField = (LinkField)item.Fields[fieldName];
            string result = defaultValue;

            if (linkField == null)
            {
                return result;
            }

            if (linkField.IsInternal || linkField.IsMediaLink)
            {
                if (linkField.TargetItem != null)
                {
                    result = LinkManager.GetItemUrl(linkField.TargetItem);
                }
                else
                {
                    result = linkField.Url;
                }
            }
            else
            {
                result = linkField.Url;
            }

            return result;
        }

        public static int GetIntValueFromField(this Item item, string fieldName)
        {
            return item.GetIntValueFromField(fieldName, 0);
        }

        public static int? GetIntValueFromField(this Item item, string fieldName, int? defaultValue)
        {
            if (item == null)
            {
                Log.Error("ItemsHelper.GetIntValueFromField(): item is null", typeof(ItemsHelper));
                throw new ArgumentNullException("item");
            }

            var field = item.Fields[fieldName];

            int? result;
            int value;

            if (field != null && int.TryParse(field.Value, out value))
            {
                result = value;
            }
            else
            {
                result = defaultValue;
            }

            return result;
        }

        public static int GetIntValueFromField(this Item item, string fieldName, int defaultValue)
        {
            if (item == null)
            {
                Log.Error("ItemsHelper.GetIntValueFromField(): item is null", typeof(ItemsHelper));
                throw new ArgumentNullException("item");
            }

            var field = item.Fields[fieldName];

            int result;
            int value;

            if (field != null && int.TryParse(field.Value, out value))
            {
                result = value;
            }
            else
            {
                result = defaultValue;
            }

            return result;
        }

        public static string GetStringValueFromField(this Item item, string fieldName)
        {
            return item.GetStringValueFromField(fieldName, string.Empty);
        }

        public static string GetStringValueFromField(this Item item, string fieldName, string defaultValue)
        {
            if (item == null)
            {
                Log.Error("ItemsHelper.GetStringValueFromField(): item is null", typeof(ItemsHelper));
                throw new ArgumentNullException("item");
            }

            var fld = item.Fields[fieldName];
            if (fld == null)
                return defaultValue;
            else
                return fld.Value;
        }

        public static bool GetBoolValueFromField(this Item item, string fieldName)
        {
            return item.GetBoolValueFromField(fieldName, false);
        }

        public static bool GetBoolValueFromField(this Item item, string fieldName, bool defaultValue)
        {
            if (item == null)
            {
                Log.Error("ItemsHelper.GetBoolIsValueFromField(): item is null", typeof(ItemsHelper));
                throw new ArgumentNullException("item");
            }

            var fld = (CheckboxField)item.Fields[fieldName];

            if (fld != null)
                return fld.Checked;
            else
                return defaultValue;
        }

        public static Item[] GetItemsFromMultilistField(this Item item, string fieldName)
        {
            return item.GetItemsFromMultilistField(fieldName, new Item[0]);
        }

        public static Item[] GetItemsFromMultilistField(this Item item, string fieldName, Item[] defaultValue)
        {
            if (item == null)
            {
                Log.Error("ItemsHelper.GetBoolIsValueFromField(): item is null", typeof(ItemsHelper));
                throw new ArgumentNullException("item");
            }

            var fld = (MultilistField)item.Fields[fieldName];

            if (fld != null)
                return fld.GetItems();
            else
                return defaultValue;
        }
    }
}